//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Controls;

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    public partial class ChartPage : System.Windows.Forms.UserControl
    {
        private Dictionary<string, Series> lines = new Dictionary<string, Series>();

        private ChartArea chartArea;

        public ChartPage()
        {
            InitializeComponent();
            InitializeChart();
        }

        public string XAxisLabel
        {
            get
            {
                return this.chartArea.AxisX.Title;
            }

            set
            {
                this.chartArea.AxisX.Title = value;
            }
        }

        public string YAxisLabel
        {
            get
            {
                return this.chartArea.AxisY.Title;
            }

            set
            {
                this.chartArea.AxisY.Title = value;
            }
        }


        private void InitializeChart()
        {
            chartArea = chart1.ChartAreas[0];
            chartArea.BackColor = Color.White;
            chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(0, 128, 64);
            chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(0, 128, 64);

            this.ClearChart();
        }

        public void RemoveLine(string activityName)
        {
            lines.Remove(activityName);
        }

        public void AddPointToLine(string activityName, double valueOfX, double valueOfY)
        {
            if (!lines.Keys.Contains(activityName))
            {
                Series series = new Series(activityName);
                series.ChartType = SeriesChartType.Line;
                series.BorderWidth = 2;

                lines.Add(activityName, series);
                chart1.Series.Add(series);
            }
            
            lines[activityName].Points.AddXY(valueOfX, valueOfY);
        }

        public void ClearChart()
        {
            lines.Clear();
            chart1.Series.Clear();
        }
    }
}
